package org.example.ding.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import org.example.ding.entity.DishCategory;

import java.time.LocalDateTime;

/**
 * 菜品分类数据传输对象
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DishCategoryDto {

    private Long id;

    @NotBlank(message = "分类名称不能为空")
    @Size(max = 50, message = "分类名称长度不能超过50个字符")
    private String name;

    @Size(max = 500, message = "分类描述长度不能超过500个字符")
    private String description;

    private DishCategory.MealType mealType;

    private String mealTypeDescription;

    private LocalDateTime createdAt;

    private Integer dishCount; // 该分类下的菜品数量

    /**
     * 从DishCategory实体创建DTO
     */
    public static DishCategoryDto fromDishCategory(DishCategory category) {
        DishCategoryDto dto = new DishCategoryDto();
        dto.setId(category.getId());
        dto.setName(category.getName());
        dto.setDescription(category.getDescription());
        dto.setMealType(category.getMealType());
        dto.setMealTypeDescription(category.getMealType() != null ? 
            category.getMealType().getDescription() : null);
        dto.setCreatedAt(category.getCreatedAt());
        
        // 菜品数量需要在Service层设置
        dto.setDishCount(category.getDishes() != null ? category.getDishes().size() : 0);
        
        return dto;
    }

    /**
     * 转换为DishCategory实体
     */
    public DishCategory toDishCategory() {
        DishCategory category = new DishCategory();
        category.setId(this.id);
        category.setName(this.name);
        category.setDescription(this.description);
        category.setMealType(this.mealType);
        return category;
    }

    /**
     * 更新DishCategory实体
     */
    public void updateDishCategory(DishCategory category) {
        if (name != null) category.setName(name);
        if (description != null) category.setDescription(description);
        if (mealType != null) category.setMealType(mealType);
    }

    /**
     * 获取餐别类型描述
     */
    public String getMealTypeDescription() {
        return mealType != null ? mealType.getDescription() : "未分类";
    }
}
